﻿using System;
using System.Diagnostics;
using SourceGenerator.AOP;


namespace SourceGenerator.AOP
{
    public class testAOPAttribute : AopBaseAttribute
    {

        Stopwatch stopWatch = new Stopwatch();
        public override void ExcuteAfter(AopMethod method, Exception ex)
        {
            TimeSpan ts = stopWatch.Elapsed;
            
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:000}",
           ts.Hours, ts.Minutes, ts.Seconds,
           ts.Milliseconds);
            Console.WriteLine($"结束执行{method.MethodTarget} {elapsedTime}");
        }

        public override void ExcuteBefore()
        {
            Console.WriteLine("开始执行");
        }

        public override void ExcuteMiddle(AopMethod method)
        {
            try
            {
                
                stopWatch.Start();
                ExcuteBefore();
                method.Excute();
                Thread.Sleep(1234);
                stopWatch.Stop();
            }
            catch (Exception ex)
            {
                ExcuteAfter(method, ex);
            }
        }
    }
}

